package com.jz.community.modulemine.money.ui;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.gyf.barlibrary.ImmersionBar;
import com.jakewharton.rxbinding2.view.RxView;
import com.jz.community.basecomm.base.BaseMvpActivity;
import com.jz.community.basecomm.glideConfig.GlideApp;
import com.jz.community.basecomm.routerUtils.RouterIntentConstant;
import com.jz.community.basecomm.utils.RxDataTool;
import com.jz.community.basecomm.utils.RxRegTool;
import com.jz.community.commview.callback.ErrorCallback;
import com.jz.community.commview.callback.LoadingCallback;
import com.jz.community.modulemine.R;
import com.jz.community.modulemine.R2;
import com.jz.community.modulemine.money.bean.PurchaseInfoBean;
import com.jz.community.modulemine.money.presenter.MoneyPurchasePresenter;
import com.kingja.loadsir.callback.Callback;
import com.kingja.loadsir.core.LoadService;
import com.kingja.loadsir.core.LoadSir;
import com.qmuiteam.qmui.util.QMUIKeyboardHelper;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = RouterIntentConstant.MONEY_PURCHASE)
public class MoneyPurchaseActivity extends BaseMvpActivity<MoneyPurchaseView.View, MoneyPurchasePresenter> implements MoneyPurchaseView.View {

    @BindView(R2.id.purchase_back)
    ImageView purchaseBack;
    @BindView(R2.id.purchase_title)
    TextView purchaseTitle;
    @BindView(R2.id.purchase_toolbar)
    Toolbar purchaseToolbar;
    //公司
    @BindView(R2.id.purchase_edit1)
    EditText purchaseEdit1;
    //数量
    @BindView(R2.id.purchase_edit2)
    EditText purchaseEdit2;
    //姓名
    @BindView(R2.id.purchase_edit3)
    EditText purchaseEdit3;
    //电话
    @BindView(R2.id.purchase_edit4)
    EditText purchaseEdit4;
    //地址
    @BindView(R2.id.purchase_edit5)
    EditText purchaseEdit5;
    //邮箱
    @BindView(R2.id.purchase_edit6)
    EditText purchaseEdit6;
    //提交
    @BindView(R2.id.purchase_enter)
    TextView purchaseEnter;
    //继续采购
    @BindView(R2.id.purchase_continue)
    TextView purchaseContinue;
    @BindView(R2.id.purchase_scroll)
    ScrollView purchaseScroll;

    private LoadService loadService;

    //是否是继续采购
    private int fromType = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getContentView() {
        return R.layout.module_mine_activity_money_purchase;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected MoneyPurchasePresenter createPresenter() {
        return new MoneyPurchasePresenter(this);
    }

    @Override
    protected void initView() {
        super.initView();
        ImmersionBar.with(this)
                .titleBar(purchaseToolbar)
                .statusBarDarkFont(true, 0.2f)//当白色背景状态栏遇到不能改变状态栏字体为深色的设备时，解决方案
                .keyboardEnable(true)
                .init();

        purchaseTitle.setText("企业采购");

        fromType = getIntent().getIntExtra("fromType", 0);

        loadService = LoadSir.getDefault().register(purchaseScroll, (Callback.OnReloadListener) v -> {
            if (loadService.getCurrentCallback() == ErrorCallback.class) {
                loadService.showCallback(LoadingCallback.class);
                mPresenter.initPurchaseData();
            }

        });

        loadService.setCallBack(ErrorCallback.class, (context, view) -> {
            ImageView mIvError = (ImageView) view.findViewById(R.id.error_img);
            mIvError.setImageResource(R.mipmap.error_default);

        });

        loadService.setCallBack(LoadingCallback.class, (context, view) -> {
            ImageView mIvLoading = (ImageView) view.findViewById(R.id.loading_img);
            GlideApp.with(mActivity)
                    .asGif()
                    .load(R.drawable.loading_default)
                    .into(mIvLoading);

        });

    }

    @SuppressLint("CheckResult")
    @Override
    protected void initDatas() {
        super.initDatas();
        if (fromType == 0) {
            mPresenter.initPurchaseData();
        } else {
            loadService.showSuccess();
        }

        RxView.clicks(purchaseEnter)
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(o -> {
                    //公司名称
                    String com_name = purchaseEdit1.getText().toString().trim();
                    //数量
                    String num = purchaseEdit2.getText().toString().trim();
                    //联系人
                    String contact_name = purchaseEdit3.getText().toString().trim();
                    //电话
                    String contact_tel = purchaseEdit4.getText().toString().trim();
                    //地址
                    String address = purchaseEdit5.getText().toString().trim();
                    //邮箱
                    String email = purchaseEdit6.getText().toString().trim();

                    if (!RxDataTool.isNullString(com_name)) {
                        if (!RxDataTool.isNullString(num)) {
                            int nums = RxDataTool.stringToInt(num);
                            if (nums < 20) {
                                showNormal("20张起购");
                            } else {
                                if (!RxDataTool.isNullString(contact_name)) {
                                    if (!RxDataTool.isNullString(contact_tel)) {
                                        if (contact_tel.length() == 11) {
                                            if (!RxDataTool.isNullString(address)) {
                                                if (!RxDataTool.isNullString(email)) {
                                                    if (RxRegTool.isEmail(email)) {
                                                        showLoading();
                                                        mPresenter.purchaseMoney(com_name, num, contact_name, contact_tel, address, email);
                                                    } else {
                                                        showNormal("请输入正确的邮箱");
                                                    }

                                                }

                                            } else {
                                                showNormal("请输入正确的地址");
                                            }

                                        } else {
                                            showNormal("请输入正确的手机号");
                                        }
                                    }

                                } else {
                                    showNormal("请输入正确的联系人");
                                }
                            }

                        } else {
                            showNormal("请输入正确的数量");
                        }

                    } else {
                        showNormal("请输入公司地址");
                    }
                });
    }

    @Override
    public void initPurchaseDataSuccess(PurchaseInfoBean entity) {
        //已经有数据的时候
        loadService.showSuccess();

        purchaseEdit1.setFocusable(false);
        purchaseEdit1.setEnabled(false);
        purchaseEdit2.setFocusable(false);
        purchaseEdit3.setFocusable(false);
        purchaseEdit4.setFocusable(false);
        purchaseEdit5.setFocusable(false);
        purchaseEdit6.setFocusable(false);

        purchaseEnter.setEnabled(false);
        purchaseEnter.setBackgroundResource(R.drawable.border_recharge_enter_gray);

        //公司
        purchaseEdit1.setText(entity.getCompany());
        //数量
        purchaseEdit2.setText(entity.getCount());
        //姓名
        purchaseEdit3.setText(entity.getName());
        //电话
        purchaseEdit4.setText(entity.getPhone());
        //地址
        purchaseEdit5.setText(entity.getAddress());
        //邮箱
        purchaseEdit6.setText(entity.getEmail());

        //采购状态1-待处理 2-待打款 3-已打款 4-已发放 5-已拒绝
        int status = entity.getStatus();

        if (status == 1) {
            //待处理
            purchaseEnter.setText("审核中");
            purchaseContinue.setVisibility(View.GONE);
        } else if (status == 2 || status == 3) {
            //待打款和已打款
            purchaseEnter.setText("审核通过");
            purchaseContinue.setVisibility(View.VISIBLE);
        } else if (status == 4) {
            //已发放
            purchaseEnter.setText("兑换码已发放");
            purchaseContinue.setVisibility(View.VISIBLE);
        } else if (status == 5) {
            //审核未通过
            purchaseEnter.setText("审核未通过");
            purchaseContinue.setVisibility(View.VISIBLE);
        }

    }

    @Override
    public void noPurchaseData() {
        //没有提交过数据
        loadService.showSuccess();

        purchaseEnter.setBackgroundResource(R.drawable.border_recharge_enter_red);
        purchaseEnter.setEnabled(true);
        purchaseEnter.setText("提交");
        purchaseContinue.setVisibility(View.GONE);

    }

    @Override
    public void initPurchaseDataFail(String msg) {
        loadService.showCallback(ErrorCallback.class);

    }

    @Override
    public void purchaseMoneySuccess(PurchaseInfoBean entity) {
        dismissLoading();
        showNormal("提交成功");

        purchaseEdit1.setFocusable(false);
        purchaseEdit1.setEnabled(false);
        purchaseEdit2.setFocusable(false);
        purchaseEdit3.setFocusable(false);
        purchaseEdit4.setFocusable(false);
        purchaseEdit5.setFocusable(false);
        purchaseEdit6.setFocusable(false);

        purchaseEnter.setEnabled(false);
        purchaseEnter.setBackgroundResource(R.drawable.border_recharge_enter_gray);

        //公司
        purchaseEdit1.setText(entity.getCompany());
        //数量
        purchaseEdit2.setText(entity.getCount());
        //姓名
        purchaseEdit3.setText(entity.getName());
        //电话
        purchaseEdit4.setText(entity.getPhone());
        //地址
        purchaseEdit5.setText(entity.getAddress());
        //邮箱
        purchaseEdit6.setText(entity.getEmail());

        //采购状态1-待处理 2-待打款 3-已打款 4-已发放 5-已拒绝
        int status = entity.getStatus();

        if (status == 1) {
            //待处理
            purchaseEnter.setText("审核中");
            purchaseContinue.setVisibility(View.GONE);
        } else if (status == 2 || status == 3) {
            //待打款和已打款
            purchaseEnter.setText("审核通过");
            purchaseContinue.setVisibility(View.VISIBLE);
        } else if (status == 4) {
            //已发放
            purchaseEnter.setText("兑换码已发放");
            purchaseContinue.setVisibility(View.VISIBLE);
        } else if (status == 5) {
            //审核未通过
            purchaseEnter.setText("审核未通过");
            purchaseContinue.setVisibility(View.VISIBLE);
        }

    }

    @Override
    public void purchaseMoneyFail(String msg) {
        dismissLoading();
        showNormal("提交失败");

    }

    @OnClick(R2.id.purchase_back)
    public void backClick(View view) {
        QMUIKeyboardHelper.hideKeyboard(purchaseEdit1);
        finish();
    }

    @OnClick(R2.id.purchase_continue)
    public void continueClick(View view) {
        Intent intent = new Intent(mActivity, MoneyPurchaseActivity.class);
        intent.putExtra("fromType", 1);
        startActivity(intent);
    }

}
